import request from '../request'
import type { ApiResponse } from '../types'

export interface Goods {
  id: number
  createTime: string
  updateTime: string
  tenantId: number | null
  typeId: number
  title: string
  subTitle: string | null
  mainPic: string | null
  pics: string | null
  price: string
  sold: number
  content: string | null
  status: number
  sortNum: number
  goodsId: string
  entId: string
  goodsCode: string
  oracleGoodsName: string
  goodsSpec: string
  manufacturer: string
  unit: string
  approvalNo: string
  formula: string
  midpack: number
  qty: string
  planCate: string | null
  logogram: string
  wholLimtNum: number
  storNum: string
  isMidpackSaleDisabled: number
  gCategory: string
  qCategory: string
  oracleIsHidden: number
  isPres: number
  attribute: string
  visitCount?: number
  lastVisitTime?: string
  typeName: string
  nearestExpiredDate?: string
  batchs?: Array<{
    nearestExpiredDate: string
  }>
}

export interface GoodsListResponse {
  code: number
  list: Goods[]
  pagination: {
    page: number
    size: number
    total: number
  }
}

export interface GoodsType {
  id: number
  name: string
  parentId: number | null
  sortNum: number
  pic: string | null
  status: number
}

// 获取商品类型列表
export const getGoodsTypeList = (data: any) => {
  return request.post<GoodsType[]>('/app/goods/type/list', data)
}

// 获取商品列表
export const getGoodsList = (params:any) => {
  return request.post<GoodsListResponse>('/app/goods/info/page', params)
}

// 获取商品列表
export const getNextGoodsList = (params:any) => {
  return request.post<GoodsListResponse>('/app/goods/info/pageWithValDate', params)
}

// 获取商品详情
export const getGoodsInfo = (id: number) => {
  return request.get<Goods>('/app/goods/info/info', { params: { id } })
}

// 添加商品
export const addGoods = (data: Omit<Goods, 'id' | 'tenantId'>) => {
  return request.post<ApiResponse>('/admin/app/goods/add', data)
}

// 更新商品
export const updateGoods = (data: Partial<Goods>) => {
  return request.post<ApiResponse>('/admin/app/goods/update', data)
}

// 删除商品
export const deleteGoods = (id: number) => {
  return request.post<ApiResponse>('/admin/app/goods/delete', { id })
} 
